/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tamnhat.ws.support;

import com.tamnhat.ws.Constants;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
public class ContentUpdateSupport {
    public static String checkPartnerAndAccount(Connection cn, String partner_code, String username) {
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            pst = cn.prepareCall("select * from sec_group a, partners b,sec_user c,sec_group_user d"
                    + " where upper(b.partner_name) = upper(a.group_name) and a.group_id = d.group_id and c.user_id =  d.user_id"
                    + " and b.partner_code = ? and c.user_name = ?");
            pst.setString(1, partner_code);
            pst.setString(2, username);
            
            rs = pst.executeQuery();
            if (rs.next()){
                return Constants.SUCCESS;
            }
            return Constants.PARTNER_CODE_NOT_CORRECT_WITH_USERNAME;
        } catch (Exception e) {
            e.printStackTrace();
            return Constants.INTERAL_ERROR;
        } finally {
            Database.closeObject(rs);
            Database.closeObject(pst);
        }       
    }
    
    public static String checkProductAndPartner(Connection cn,String product_code,String partner_code){
        PreparedStatement pst = null;
        ResultSet rs = null;
        try{
            pst = cn.prepareCall("select * from sdp_product_partner a, sdp_products b, partners c"
                    + " where a.partner_id = c.partner_id and a.product_id = b.product_id"
                    + " and b.sdp_product_code = ? and c.partner_code = ?");
            
            pst.setString(1, product_code);
            pst.setString(2, partner_code);
            
            rs = pst.executeQuery();
            if(rs.next()){
                return Constants.SUCCESS;
            }
            return Constants.PRODUCT_CODE_NOT_CORRECT_WITH_PARTNER;
        }catch(Exception e){
            e.printStackTrace();
            return Constants.INTERAL_ERROR;
        }finally{
            Database.closeObject(rs);
            Database.closeObject(pst);
        }
    }
    
    public static String checkSMSType(String sms_type){
        if(!sms_type.equals("text") && !sms_type.equals("wappush")){
            return Constants.INVALID_PRODUCT_CONTENT_TYPE;
        }
        return Constants.SUCCESS;
    }
    
    public static String checkTime(String time_send_sms){
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        try{
            Date date = sdf.parse(time_send_sms);
            return Constants.SUCCESS;
        }
        catch(Exception e){
            e.printStackTrace();
            return Constants.INVALID_DATE_TIME_FORMAT;
        }        
    }
    
    public static String getProductIdByCode(Connection cn,String product_code){
        PreparedStatement pst = null;
        ResultSet rs = null;
        try{
            pst = cn.prepareCall("select product_id from sdp_products where sdp_product_code = ?");
            
            pst.setString(1, product_code);
            rs = pst.executeQuery();
            if(rs.next()){
                return rs.getString("product_id");
            }
            return "";
        }catch(Exception e){
            e.printStackTrace();
            return Constants.INTERAL_ERROR;
        }finally{
            Database.closeObject(rs);
            Database.closeObject(pst);
        }
    }
    public static String getSDPServiceIdByProductId(Connection cn,String product_id){
        PreparedStatement pst = null;
        ResultSet rs = null;
        try{
            pst = cn.prepareCall("select sdp_service_id from sdp_products a,sdp_services b "
                    + " where a.service_id = b.service_id and a.product_id = ?");
            
            pst.setInt(1, Integer.parseInt(product_id));
            rs = pst.executeQuery();
            if(rs.next()){
                return rs.getString("sdp_service_id");
            }
            return "";
        }catch(Exception e){
            e.printStackTrace();
            return Constants.INTERAL_ERROR;
        }finally{
            Database.closeObject(rs);
            Database.closeObject(pst);
        }
    }
}
